package com.fxp.article_comment.service;

import com.fxp.article_comment.dao.CommentDao;
import com.fxp.article_comment.entity.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class CommentService {
    @Autowired
    private CommentDao commentDao;
    public List<Map<String,Object>> list(){
        List<Map<String,Object>> list1=new ArrayList<>();
        Comment comment=new Comment();
        comment.setParetid("0");
        List<Comment> list=commentDao.findAll(Example.of(comment));
        Integer a=0;
        for (Comment c:list){
            Map<String,Object> map=new HashMap<>();
            map.put("luozhu",c);
            Comment comment1=new Comment();
            comment1.setParetid(list.get(a).getArticleId());
            map.put("zilou",commentDao.findAll(Example.of(comment1)));
            list1.add(map);
            a++;
        }
        return list1;
    }

    public boolean add(Comment comment){
        List<Comment> list=commentDao.findAll();
        Integer a= Integer.valueOf(list.get(list.size()-1).getArticleId());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        comment.setCreateDateTime(format.format(date));
        comment.setArticleId(""+(a+1)+"");
        comment.setLikenum(0);
        comment.setReplunum(0);
        comment.setState("1");
        System.out.println(commentDao.save(comment));
        if (comment.getParetid()!="0"){
            Comment commentAll=new Comment();
            commentAll.setParetid(comment.getParetid());
            List<Comment> list2=commentDao.findAll(Example.of(commentAll));
            Comment commentOne=new Comment();
            commentOne.setArticleId(comment.getParetid());
            List<Comment> list3=commentDao.findAll(Example.of(commentOne));
            commentOne=list3.get(0);
            commentOne.setReplunum(list2.size());
            commentDao.save(commentOne);
        }
        return true;
    }

    public boolean del(String id){
        Comment comment=new Comment();
        comment.set_id(id);
        List<Comment> list=commentDao.findAll(Example.of(comment));
        if (list==null){
            return false;
        }
        commentDao.deleteById(id);
        return true;
    }

    public boolean update(String id){
        Comment comment=new Comment();
        comment.set_id(id);
        List<Comment> list=commentDao.findAll(Example.of(comment));
        if (list==null){
            return false;
        }
        Comment comment1=list.get(0);
        comment1.set_id(id);
        comment1.setLikenum(list.get(0).getLikenum()+1);
        System.out.println(commentDao.save(comment1));
        return true;
    }
}
